package com.lancoo.ccas53.arrangecourse.coursescheduling.campusscheduling;

import com.lancoo.ccas53.arrangecourse.coursescheduling.allocatetimeslotroom.AllocateNoClashTimeslotRoom;
import com.lancoo.ccas53.arrangecourse.coursescheduling.allocatetimeslotroom.AllocateTimeslotRoom;
import com.lancoo.ccas53.arrangecourse.coursescheduling.assignlist.NoClashAssign;
import com.lancoo.ccas53.arrangecourse.entities.ClassHour;
import com.lancoo.ccas53.arrangecourse.entities.TeachingClassUnit;
import com.lancoo.ccas53.arrangecourse.entities.TimeslotRoom;
import com.lancoo.ccas53.arrangecourse.rulecheck.RuleInit;
import com.lancoo.ccas53.pojo.dto.ArrangeTeachingClassDto;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

/**
 * @Description 指定教室排课
 * 针对有些教学班，需要安排在指定的教室，进行单独的排课处理
 * @Author Fortysun
 * @Date 2023/8/18 16:45
 **/
@Slf4j
public class FixRoomScheduling extends FixScheduling {

    public FixRoomScheduling(RuleInit ruleInit) {
        super(ruleInit);
        AllocateTimeslotRoom allocateTimeslotRoom = new AllocateNoClashTimeslotRoom();
        super.normalAssign = new NoClashAssign(ruleInit.getNoClashBaseRule(), allocateTimeslotRoom);
    }
    /**
     * 处理尽量排排课，一定排课的内容
     * @param taskId 排课计划id
     * @param teachingClassList 教学班列表
     * @param timeslotRoomList 教室课时分组列表
     * @param leaveList
     * @param classHourMap 锁定的课表信息
     * @return
     */
    public LinkedList<TeachingClassUnit> start(Integer taskId,
                                               List<ArrangeTeachingClassDto> teachingClassList,
                                               LinkedList<TimeslotRoom> timeslotRoomList,
                                               LinkedList<TeachingClassUnit> leaveList,
                                               ConcurrentHashMap<String, CopyOnWriteArrayList<ClassHour>> classHourMap) {
        if ( (teachingClassList != null && !teachingClassList.isEmpty())
                || (leaveList != null && !leaveList.isEmpty()) ) {
            //找出设置了指定教室的教学班列表信息
            List<ArrangeTeachingClassDto> fixRoomTeachingClassList = teachingClassList.stream()
                    .filter(teachingClass -> teachingClass.getRoomId()!=null)
                    .collect(Collectors.toList());

            LinkedList<TeachingClassUnit> fixRoomTeachingClassUnitList = leaveList.stream()
                    .filter(o -> o.getRoomId() != null).collect(Collectors.toCollection(LinkedList::new));

            if ( (fixRoomTeachingClassList != null && !fixRoomTeachingClassList.isEmpty())
                    || (fixRoomTeachingClassUnitList != null && !fixRoomTeachingClassUnitList.isEmpty())
                ) {
                log.info("FixRoomScheduling--start:指定教室排课的教学班数量为："
                        +(fixRoomTeachingClassList!=null?fixRoomTeachingClassList.size():0));
                //从总教学班列表，去掉固定教室排课的教学班列表信息
                teachingClassList.removeAll(fixRoomTeachingClassList);

                log.info("FixRoomScheduling--start:指定教室排课的排课单元数量为："
                        +(fixRoomTeachingClassUnitList!=null?fixRoomTeachingClassUnitList.size():0));
                leaveList.removeAll(fixRoomTeachingClassUnitList);

                //开始指定了教室的教学班的排课
                return startFixCourse(taskId, fixRoomTeachingClassList, fixRoomTeachingClassUnitList, timeslotRoomList, classHourMap);
            }
            log.info("FixRoomScheduling--start:指定教室排课的教学班数量为0");
        }
        log.info("FixRoomScheduling--start:剩余需要处理的教学班数量为0~！");
        return null;
    }

}
